﻿/*
 * User: RSny
 * Date: 5/10/2012
 * Time: 9:53 AM
 */
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Aspects.Custom.Audio
{
    public class WaveInBuffer : WaveBuffer
    {
		public WaveInBuffer(IntPtr waveInHandle, int size) : base(waveInHandle, size, true){}

		public bool Record()
		{
			lock(this)
			{
				SoundEvent.Reset();
				Busy = WaveNative.waveInAddBuffer(WavePointer, ref Header, Marshal.SizeOf(Header)) == MMSYSERROR.MMSYSERR_NOERROR;
				return Busy;
			}
		}
		
		public void Reuse()
		{
		    WaveHelper.Try(WaveNative.waveInUnprepareHeader(WavePointer, ref Header, Marshal.SizeOf(Header)));
            WaveHelper.Try(WaveNative.waveInPrepareHeader(WavePointer, ref Header, Marshal.SizeOf(Header)));
            WaveHelper.Try(WaveNative.waveInAddBuffer(WavePointer, ref Header, Marshal.SizeOf(Header)));
		}
    }
}